import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class MaxHeightAndScrollPage extends StatefulWidget {
  const MaxHeightAndScrollPage({super.key});

  @override
  State<MaxHeightAndScrollPage> createState() => _MaxHeightAndScrollPageState();
}

class _MaxHeightAndScrollPageState extends State<MaxHeightAndScrollPage> {

  int itemCount = 3;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Flutter demo")),
      body: Stack(
        children: [
          Positioned(
            bottom: 0,
            child: Container(
              color: Colors.lightGreen[200],
              constraints: const BoxConstraints(
                maxHeight: 500,
              ),
              width: MediaQuery.of(context).size.width,
              child: CustomScrollView(
                shrinkWrap: true,
                physics: const BouncingScrollPhysics(),
                slivers: <Widget>[
                  ///复杂的标题
                  buildSliverAppBar(),
                  
                  ///间距
                  const SliverPadding(
                    padding: EdgeInsets.all(5),
                  ),
                  ///列表
                  buildSliverFixedExtentList(),
                  
                  ///间距
                  const SliverPadding(
                    padding: EdgeInsets.all(5),
                  ),

                  ///九宫格
                  buildSliverGrid(),
                ],
              ),
            )
          )
        ]
      )
    );
  }

   SliverAppBar buildSliverAppBar() {
    return SliverAppBar(
      title: GestureDetector(
        onTap: () {
          int num = Random().nextInt(10) + 3;
          if (kDebugMode) {
            print(num);
          }
          setState(() {
            itemCount = num;
          });
        },
        child: const Text("讲解组合滑动"),
      ),
    );
  }

  SliverGrid buildSliverGrid() {
    return SliverGrid(
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        ///九宫格的列数
        crossAxisCount: 3,
        ///子Widget 宽与高的比值
        childAspectRatio: 2.0,
        ///主方向的 两个 子Widget 之间的间距
        mainAxisSpacing: 10,
        /// 次方向 子Widget 之间的间距
        crossAxisSpacing: 10,
      ),
      ///子Item构建器
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, num index) {
          ///每一个子Item的样式
          return Container(
            color: Colors.blue,
            child: Text("grid $index"),
          );
        },
        ///子Item的个数
        childCount: 5,
      ),
    );
  }
  SliverFixedExtentList buildSliverFixedExtentList() {
    return SliverFixedExtentList(
      ///子条目的高度
      itemExtent: 40,
      ///子条目布局构建代理
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, num index) {
          ///子条目的布局样式
          return Container(
            color: Colors.red,
            margin: const EdgeInsets.only(bottom: 10),
            child: Text("list $index"),
          );
        },
        ///子条目的个数
        childCount: itemCount,
      ),
    );
  }

}
